package servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.contents;

import util.Conn;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class AddServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;character=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			String content = request.getParameter("content");//获取留言内容参数
			content = URLDecoder.decode(content, "UTF-8");//解码
			contents c = Conn.addContens(content);//向数据库加内容
			JSONObject jb = new JSONObject(); 
			jb.put("status", "OK");
			jb.put("id", c.getId());
			response.getWriter().write(jb.toString());	//输出
		
	}catch (ClassNotFoundException | SQLException e) {
		// TODO Auto-generated catch block
		JSONObject jb = new JSONObject();
		jb.put("status", "fail");	
		e.printStackTrace();
	}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
